//
//  JQHomePageViewController.m
//  XLPageViewControllerExample
//
//  Created by Bruce on 2019/5/10.
//  Copyright © 2019 JQLive. All rights reserved.
//

#import "JQHomePageViewController.h"
#import "XLPageViewController.h"
#import "JQHomeItemVC.h"
#import "JQHomeOtherItemVC.h"
#import "JQHomeRoomTagsModel.h"

@interface JQHomePageViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@end

@implementation JQHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 加载 获取 titles 数组
//    [self roomTagsLoad];
    
    #pragma mark - 设置 2 效果
    UIImageView *topImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 125)];
    //    [[UIApplication sharedApplication].keyWindow addSubview:topImageV];
//        [self.navigationController.navigationBar addSubview:topImageV];
    [self.view addSubview:topImageV];
    topImageV.image = [UIImage imageNamed:@"home_background"];
    topImageV.backgroundColor = MYHome_backgroundColor;
    
    // 用于首页下拉操作，看到不是空白底层
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 125, SCREEN_WIDTH, 44)];
    [self.view addSubview:view];
    view.backgroundColor = MYHome_backgroundColor;
    
    self.view.backgroundColor = [UIColor clearColor];
//    self.view.backgroundColor = MYHome_backgroundColor;//[UIColor clearColor];
    
    // 加载分页控制器
//    [self initPageViewController];
    // 请求 分页管理title 数组
    [self roomTagsLoad];
    
    // 设置 leftbtn
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 96, (26));
//    [leftBtn setBackgroundImage:[UIImage svgImageNamed:@"home_liveIcon" size:CGSizeMake(96, 26)] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"home_liveIcon"] forState:UIControlStateNormal];

//    [leftBtn addTarget:self action:@selector(setBackVC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * Item = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItems = @[Item];
    
}
// 请求 导航栏 数据
- (void)roomTagsLoad{
    [MYRequesTool MY_live_roomtags_RequestWithParam:nil successHandle:^(id  _Nonnull response) {
        NSLog(@"response0:%@", response);
        
        JQHomeRoomTagsModel *model = [JQHomeRoomTagsModel mj_objectWithKeyValues:response];
        if (model.code == 200) {
            NSMutableArray *nameArrM = [NSMutableArray arrayWithObject:@"全部"];
            NSMutableArray *roomTagIdArrM = [NSMutableArray arrayWithObject:@"0"];
            for (JQHomeRoomTagsData *data in model.data) {
                [nameArrM addObject:data.name];
                [roomTagIdArrM addObject:data.tag_id];
            }
            self.titles = [nameArrM copy];
            self.roomTagIdArr = [roomTagIdArrM copy];
            // 刷新视图
//            [self setUpSubNav];
            // 加载分页控制器
                [self initPageViewController];
        }
    } failureHandle:^(NSError * _Nonnull error) {
        NSLog(@"error：%@", error);
    }];
}
// leftBtn方法响应 暂不使用
- (void)setBackVC{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - InitNAV
///// 设置导航栏的背景图，默认为 NavBarBackgroundImage
- (nullable UIImage *)navigationBarBackgroundImage{
//    return [UIImage new];
#pragma mark - 设置 导航栏的背景图  效果
     return [UIImage imageNamed:@"home_background"]; //这里的timg就是那张叶子图
}

/// 设置当前导航栏的 barTintColor，默认为 NavBarBarTintColor
- (nullable UIColor *)navigationBarBarTintColor{
//    return [UIColor clearColor];
    #pragma mark - 设置 导航栏的背景 颜色
    return MYHome_backgroundColor;
}
//// 系统状态栏
//- (UIStatusBarStyle)preferredStatusBarStyle{
//    return UIStatusBarStyleLightContent;
//}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

}
// 分页控制器管理
- (void)initPageViewController {
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:self.config];
    self.pageViewController.view.frame = self.view.bounds;
//    self.pageViewController.view.backgroundColor = MYHome_backgroundColor;
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
}
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    if (index == 0) {
        JQHomeItemVC *homeItemVC = [[JQHomeItemVC alloc] init];
        return homeItemVC;
    }else{
        JQHomeOtherItemVC *otherItemVC = [[JQHomeOtherItemVC alloc] init];
        otherItemVC.roomTagId = self.roomTagIdArr[index];
        return otherItemVC;
    }
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.titles[index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.titles.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    NSLog(@"切换到了：%@",self.titles[index]);
}


@end
